package myta.bean;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.faces.component.html.HtmlInputText;

import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;
import myta.service.Utility;

public class AbsensiSiswa {
	private HtmlInputText txtthnajaran;
	private HtmlInputText txtstartdate;
	private HtmlInputText txtenddate;
	
	private DataTable datatable;
	
	public AbsensiSiswa() {
		try {
			txtthnajaran = new HtmlInputText();
			txtstartdate = new HtmlInputText();
			txtenddate = new HtmlInputText();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			txtthnajaran.setValue(sdf.format(Calendar.getInstance().getTime()));
			
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			txtstartdate.setValue(sdf.format(Calendar.getInstance().getTime()));
			txtenddate.setValue(sdf.format(Calendar.getInstance().getTime()));
		} catch (Exception e) {
			Utility.showMessage(e.getMessage());
		}
	}
	
	private void loadDatatable(String thnajaran, String nis, String idkelas, String startdate, String enddate) throws Exception{
		datatable = new DataTable();
		
		String sql = "select distinct sm.thnajaran, s.nis, s.nama, a.tanggal, m.namamapel, ";
		sql += "case when ad.absensi = 0 then 'Tidak Masuk' else 'Masuk' end as absensi from absen a ";
		sql += "inner join absen_detail ad on a.idabsen = ad.idabsen ";
		sql += "inner join siswa s on ad.nis = s.nis ";
		sql += "inner join mapel m on a.idmapel = m.idmapel ";
		sql += "inner join kelassiswa ks on a.idkelassiswa = ks.idkelassiswa ";
		sql += "inner join kelas k on ks.idkelas = ks.idkelas ";
		sql += "inner join semester sm on k.idsemester = sm.idsemester ";
		sql += "where sm.thnajaran = ? and s.nis like ? and k.idkelas like ? and (a.tanggal >= ? and a.tanggal <= ?) ";
		sql += "order by sm.thnajaran, s.nis, s.nama, a.tanggal";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, Integer.valueOf(thnajaran));
		command.setObject(2, nis.equalsIgnoreCase("-")?"%%":"%"+nis+"%");
		command.setObject(3, idkelas.equalsIgnoreCase("-")?"%%":"%"+idkelas+"%");
		command.setObject(4, startdate);
		command.setObject(5, enddate);
		
		datatable = command.executeQuery();
	}
	
	public void proses(){
		try {
			SessionSiswa s = (SessionSiswa)Utility.getBean("sessionSiswa");
			loadDatatable(txtthnajaran.getValue().toString(), s.getNis(), this.getIdKelas(), txtstartdate.getValue().toString(), txtenddate.getValue().toString());
		} catch (Exception e) {
			Utility.showMessage(e.getMessage());
		}
	}
	
	private String getIdKelas() throws Exception{
		SessionSiswa s = (SessionSiswa)Utility.getBean("sessionSiswa");
		String sql = "select k.idkelas from kelassiswa ks inner join kelassiswa_detail ksd on ks.idkelassiswa = ksd.idkelassiswa inner join kelas k on ks.idkelas = k.idkelas inner join semester s on k.idsemester = s.idsemester where ksd.nis = ? and s.thnajaran = ?";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, s.getNis());
		command.setObject(2, Integer.valueOf(txtthnajaran.getValue().toString()));
		DataTable datatable = command.executeQuery();
		return datatable.getRow(0).getString("idkelas");
	}

	public HtmlInputText getTxtthnajaran() {
		return txtthnajaran;
	}

	public void setTxtthnajaran(HtmlInputText txtthnajaran) {
		this.txtthnajaran = txtthnajaran;
	}

	public DataTable getDatatable() {
		return datatable;
	}

	public void setDatatable(DataTable datatable) {
		this.datatable = datatable;
	}

	public HtmlInputText getTxtstartdate() {
		return txtstartdate;
	}

	public void setTxtstartdate(HtmlInputText txtstartdate) {
		this.txtstartdate = txtstartdate;
	}

	public HtmlInputText getTxtenddate() {
		return txtenddate;
	}

	public void setTxtenddate(HtmlInputText txtenddate) {
		this.txtenddate = txtenddate;
	}
}
